package com.irdstudio.efp.esb.service.impl.yx;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.irdstudio.efp.esb.common.constant.MsLoanConstant;
import com.irdstudio.efp.esb.service.bo.req.yx.LetterReq;
import com.irdstudio.efp.esb.service.bo.resp.yx.LetterResp;
import com.irdstudio.efp.esb.service.client.ESBClientFactory;
import com.irdstudio.efp.esb.service.client.impl.MSESBBeanCreator;
import com.irdstudio.efp.esb.service.facade.yx.LetterService;
import com.irdstudio.basic.framework.core.base.FrameworkService;
import com.irdstudio.basic.framework.core.exception.ESBException;
import com.irdstudio.basic.framework.core.util.SpringContextUtils;

/**
 * 用信审批通知接口
  * @author heyingfeng
  * @since 2019年3月7日 下午3:10:08
  * @version 1.0
 */
@Service("letterService")
public class LetterServiceImpl implements LetterService, FrameworkService {

	private static Logger log = LoggerFactory.getLogger(LetterServiceImpl.class);

	// 发送请求
	@Override
	public LetterResp letterNotify(LetterReq letterReq) throws Exception {
		LetterResp letterResp = null;
		try {
			letterResp = ESBClientFactory.buildClient().withBody(letterReq).withTradeNo(MsLoanConstant.TRADE_NO_30220007)
					.withScene(MsLoanConstant.SCENE_03)
					.withESBBeanFactory((MSESBBeanCreator) SpringContextUtils.getBean("MSESBBeanCreator"))
					.create().sendAndReceive().getBody(LetterResp.class);
			if(letterResp!=null) {
				if ("0".equals(letterResp.getRetCode()) || "E1001".equals(letterResp.getRetCode())) {
					log.info("用信审批结果消费接口调用，成功返回，开始进行后处理 ，请求参数： " + letterReq + ", 返回结果： " + letterResp);
					return letterResp;
				}
			}
			log.error("用信审批结果消费接口调用失败！,返回结果： " + letterReq);
			throw new ESBException();
		} catch (Exception e) {
			log.error("用信审批结果消费接口调用失败！"+e.getMessage());
			e.printStackTrace();
			throw e;
		}
	}

}
